package GUI.servidor;

import java.awt.Color;
import java.awt.Component;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Handler;

import javax.naming.CommunicationException;

import dados.Arquivo;
import dados.Usuario;

import Protocolo.MiniTCP;

import info.monitorenter.gui.chart.Chart2D;
import info.monitorenter.gui.chart.ITrace2D;
import info.monitorenter.gui.chart.traces.Trace2DLtd;


public class GraficoVelocidadeServidor {

	private Chart2D chart;
	private Timer timer;
	private TimerTask task;
	private long m_starttime;
	private Janela_Servidor jan;
	private Color[] cor = {Color.RED, Color.BLUE, Color.GREEN, Color.GRAY, Color.ORANGE, Color.DARK_GRAY};
	private HashMap<Usuario, Color> userColor;
	private HashMap<Color, Integer> colorInd;
	private ITrace2D[] traceArray;

	public GraficoVelocidadeServidor(Janela_Servidor jan, double m_y){
		this.chart = new Chart2D();
		this.timer = new Timer(true);
		this.jan = jan;
		this.traceArray = new ITrace2D[cor.length];
		this.userColor = new HashMap<Usuario, Color>();
		this.colorInd = new HashMap<Color, Integer>();
		colorInd.put(Color.RED, 0);
		colorInd.put(Color.BLUE, 1);
		colorInd.put(Color.GREEN, 2);
		colorInd.put(Color.GRAY, 3);
		colorInd.put(Color.ORANGE, 4);
		colorInd.put(Color.DARK_GRAY, 5);
	}

	public Component montarGrafico(){
		chart.setName("Velocidade Clientes");
		for (int i = 0; i < cor.length; i++) {
			traceArray[i] = new Trace2DLtd(200);
			traceArray[i].setColor(cor[i]);
			traceArray[i].setName("\n");
			chart.addTrace(traceArray[i]);
		}

		TimerTask task = new TimerTask(){
			private double m_starttime  = System.currentTimeMillis();;

			@Override
			public void run() {
				try{
					ArrayList<Usuario> array = jan.getServidor().repositorioUsuario.getRepositorioUsuario();
					
					for (Usuario user : userColor.keySet()) {
						if (!array.contains(user) || !user.isConectado()) {
							traceArray[colorInd.get(userColor.remove(user))].setName("\n");
						}
					}
					
					for (Usuario user : array) {
						if (user.isConectado()) {
							if (!userColor.containsKey(user)) {
								for (Color color : cor) {
									if (!userColor.containsValue(color)) {
										userColor.put(user, color);
										traceArray[colorInd.get(color)].setName(user.getNome());
										break;
									}
								}
							}
							double m_y = 0;

							ArrayList<Arquivo> arquivos = user.getRepositorioUsuarArq().repositorioArquivo;
							for (int k = 0; k < arquivos.size(); k++) {
								if(arquivos.get(k).isSyncing()){
									m_y = arquivos.get(k).getVelocidade()/1024;
									traceArray[colorInd.get(userColor.get(user))].addPoint(((double) System.currentTimeMillis() - this.m_starttime), m_y);
								}
							}
						}
					}
				} catch (NullPointerException e) {
				}
			}

		};
		timer.schedule(task, 1000, 1000);
		return chart;
	}
}